/**
 * @project readnote
 * @Title BookServiceImpl.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.book.service.impl;

import java.util.List;

import com.cr.backbone.hibenate.AppBaseServiceImpl;
import com.cr.backbone.util.UtilsDateTime;
import com.readnote.book.dao.UserBookDAO;
import com.readnote.book.dao.UserNoteDAO;
import com.readnote.book.model.Book;
import com.readnote.book.model.UserBook;
import com.readnote.book.model.UserNote;
import com.readnote.book.service.UserNoteService;
import com.readnote.user.model.User;



public class UserNoteServiceImpl extends AppBaseServiceImpl<UserNote> implements UserNoteService {
	
	private UserBookDAO userBookDAO;
	
	public UserNoteDAO getDAO(){
		return ((UserNoteDAO)(dao));
	}

	/**
	 * 添加笔记
	 * @param userNote
	 * @return
	 */
	public Long saveAddUserNote(UserNote userNote){
		UserBook ub = userBookDAO.get(userNote.getUserBook().getId());
		User user=new User (ub.getUser().getId());;
		Book book=new Book(ub.getBook().getId());
		
		userNote.setUser(user);
		userNote.setBook(book);
		userNote.setCreateDate(UtilsDateTime.getCurrentDate());
		return getDAO().save(userNote);
	}
	
	/**
	 * 更新读书笔记信息
	 * @param userNote
	 * @return
	 */
	public Long updateUserNote(UserNote userNote){
		Long id=userNote.getId();
		UserBook ub = userBookDAO.get(userNote.getUserBook().getId());
		User user=new User (ub.getUser().getId());;
		Book book=new Book(ub.getBook().getId());
		userNote.setUser(user);
		userNote.setBook(book);
		userNote.setUpdateDate(UtilsDateTime.getCurrentDate());
		
		getDAO().update(userNote);
		return id;
	}
	
	/**
	 * 获取用户笔记集合，by用户书籍
	 * @param userBook
	 * @return
	 */
	public List<UserNote> getListUserNoteByUserBook(UserBook userBook){
		return getDAO().getListUserNoteByUserBook(userBook);
	}

	public List<UserNote> getListUserNoteByUser(User user) {
		return getDAO().getListUserNoteByUser(user);
	}
	
	/**
	 * 获取用户最后添加笔记
	 * @param user
	 * @return
	 * @author huxh
	 * @date 2012-7-5 
	 */
	public UserNote getUserNoteTopOneByUser(User user){
		return getDAO().getUserNoteTopOneByUser(user);
	}

	public UserBookDAO getUserBookDAO() {
		return userBookDAO;
	}

	public void setUserBookDAO(UserBookDAO userBookDAO) {
		this.userBookDAO = userBookDAO;
	}
	
}
